/*
 * Copyright (c) 2001, 2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */

#include "StdAfx.h"

JNIEXPORT void JNICALL Java_net_sourceforge_perforce_api_internal_Spec_construct(JNIEnv* env, jobject javaSpec)
{
	setNativeHandle(env, javaSpec, new Spec());
}

JNIEXPORT void JNICALL Java_net_sourceforge_perforce_api_internal_Spec_destruct(JNIEnv* env, jobject javaSpec)
{
	Spec* spec = (Spec*)getNativeHandle(env, javaSpec);
	
	delete spec;
	setNativeHandle(env, javaSpec, NULL);
}

JNIEXPORT void JNICALL Java_net_sourceforge_perforce_api_internal_Spec_decode(JNIEnv* env, jobject javaSpec, jstring javaSpecDefString, jobject javaError)
{
	Spec* spec = (Spec*)getNativeHandle(env, javaSpec);
	char* buf = (char*)env->GetStringUTFChars(javaSpecDefString, JNI_FALSE);
	Error* e = (Error*)getNativeHandle(env, javaError);
	
	StrRef* encoded = new StrRef(buf);
	spec->Decode(encoded, e);
	delete encoded;
}

JNIEXPORT jstring JNICALL Java_net_sourceforge_perforce_api_internal_Spec_format(JNIEnv* env, jobject javaSpec, jobject javaSpecData)
{
	Spec* spec = (Spec*)getNativeHandle(env, javaSpec);
	SpecData* data = (SpecData*)getNativeHandle(env, javaSpecData);

	StrBuf* buf = spec->Format(data);
	return env->NewStringUTF(buf->Text());
}

JNIEXPORT void JNICALL Java_net_sourceforge_perforce_api_internal_Spec_parse(JNIEnv* env, jobject javaSpec, jstring specString, jobject javaSpecData, jobject javaError, jint valid)
{
	Spec* spec = (Spec*)getNativeHandle(env, javaSpec);
	char* buf = (char*)env->GetStringUTFChars(specString, JNI_FALSE);
	SpecData* data = (SpecData*)getNativeHandle(env, javaSpecData);
	Error* e = (Error*)getNativeHandle(env, javaError);
	
	spec->Parse(buf, data, e, (int)valid);
}
